<%--
  -	$Version: 0.1 $
  -	$Date: 2008-09-09 16:48:22 $
--%>
<%@ page pageEncoding="utf-8"%>
<%@ page import="org.jawa.core.util.JawaGlobals"%>
<%@ page import="org.jawa.core.JawaServer"%>
<%@ page
	import="org.jawa.admin.util.ParamUtils,org.jawa.core.util.ClassUtils,org.jawa.core.database.*,java.io.File,java.sql.*,java.util.HashMap,java.util.Locale,java.util.Map,java.util.StringTokenizer"%>
<%
	// 系统已安装
	if (!JawaServer.getInstance().isSetupMode()) {
        response.sendRedirect("setup-completed.jsp");
        return;
    }
%>
<%
	// Get parameters
	boolean next = ParamUtils.getBooleanParameter(request, "next");

	Boolean enabled = ParamUtils.getBooleanParameter(request,"enabled",false);
	String serverName = ParamUtils.getParameter(request, "serverName");
	String multicastAddress = ParamUtils.getParameter(request, "multicastAddress");
	int multicastPort = ParamUtils.getIntParameter(request, "multicastPort",2299);
	Boolean loopBack = ParamUtils.getBooleanParameter(request, "loopBack", true);
	Boolean bootstrapping = ParamUtils.getBooleanParameter(request, "bootstrapping", false);

	Map<String, String> errors = new HashMap<String, String>();
	
	//提交保存
	if (next) {
		JawaGlobals.setXMLProperty("cache.clustering.enabled", Boolean.toString(enabled));
		
		//打开集群	
		if(enabled){
			if (serverName == null) {
				errors.put("serverName", "请输入正确的集群成员名称.");
			}
			if (multicastAddress ==null) {
				errors.put("multicastAddress", "请输入正确的集群组播地址.");
			}
			JawaGlobals.setXMLProperty("cache.clustering.serverName", serverName);
			JawaGlobals.setXMLProperty("cache.clustering.multicastAddress", multicastAddress);
			JawaGlobals.setXMLProperty("cache.clustering.multicastPort", Long.toString(multicastPort));
			JawaGlobals.setXMLProperty("cache.clustering.loopBack", Boolean.toString(loopBack));
			JawaGlobals.setXMLProperty("cache.clustering.bootstrapping", Boolean.toString(bootstrapping));
		}
		
		response.sendRedirect("setup-mail-settings.jsp");
		return;
	}
	
	if (!next) {
		 enabled = JawaGlobals.getXMLProperty("cache.clustering.enabled",false);
		 serverName = JawaGlobals.getXMLProperty("cache.clustering.serverName");
		 multicastAddress = JawaGlobals.getXMLProperty("cache.clustering.multicastAddress");
		 multicastPort = JawaGlobals.getXMLProperty("cache.clustering.multicastPort",2299);
		 loopBack = JawaGlobals.getXMLProperty("cache.clustering.loopBack",true);
		 bootstrapping = JawaGlobals.getXMLProperty("cache.clustering.bootstrapping",false);
	}
	
	if(serverName==null){
		serverName = java.net.InetAddress.getLocalHost().getHostName();
	}
	
%>

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>缓存设置</title>
		<meta name="currentStep" content="3" />
	</head>
	<body>
		<div class="row-fluid">
			<div class="page-header">
				<h3>
					缓存设置
				</h3>
			</div>
			<div class="well">
				<form name="clusterform" action="setup-clustering-settings.jsp" method="post" class="form-horizontal">
					<input type="hidden" name="next" value="true">
					<div class="control-group " >
							<label class="control-label">
								缓存类别:
							</label>
							<div class="controls">
							 	<select name="enabled" onchange="changeCache(this);">
									<option value="true"  <%=enabled?"selected=\"selected\" ":"" %> >
										&#149;
										集群缓存
									</option>
									<option value="false" <%=!enabled?" selected=\"selected\" ":"" %> >
										&#149;
										本地缓存
									</option>
								</select>
							</div>
					</div>
					<div id="clusterSeting" style="display: <%=enabled?"":"none" %>">
						<div class="control-group <%=errors.containsKey("maxConnections") ? "error" : ""%>">
							<label class="control-label">
								成员名称：
							</label>
							<div class="controls">
								<input type="text" name="serverName" size="3" maxlength="5" value="<%=serverName %>" class="input-xlarge "/>
								<%
									if (errors.get("serverName") != null) {
								%>
								<span class="help-inline"> <%=errors.get("serverName")%></span>
								<%
									}
								%>
								<span class=" help-block">在集群中成员名称必须具有唯一性.</span>
							</div>
						</div>
						<div class="control-group <%=errors.containsKey("multicastAddress") ? "error" : ""%>">
							<label class="control-label">
								组播地址：
							</label>
							<div class="controls">
								<input type="text" name="multicastAddress" size="15" maxlength="15" value="<%=multicastAddress %>" class="input-xlarge "/>
								<%
									if (errors.get("multicastAddress") != null) {
								%>
								<span class="help-inline"> <%=errors.get("multicastAddress")%></span>
								<%
									}
								%>
							</div>
						</div>
						<div class="control-group <%=errors.containsKey("multicastPort") ? "error" : ""%>">
							<label class="control-label">
								组播端口：
							</label>
							<div class="controls">
								<input type="text" name="multicastPort" size="4" maxlength="4" value="<%=multicastPort %>" class="input-xlarge "/>
								<%
									if (errors.get("multicastPort") != null) {
								%>
								<span class="help-inline"> <%=errors.get("multicastPort")%></span>
								<%
									}
								%>
							</div>
						</div>
						<div class="control-group <%=errors.containsKey("loopBack") ? "error" : ""%>">
							<label class="control-label">
								生成环回：
							</label>
							<div class="controls">
								<select name="loopBack">
									<option value="true"  <%=loopBack?"selected=\"selected\" ":"" %> >
										&#149;
										是
									</option>
									<option value="false" <%=!loopBack?" selected=\"selected\" ":"" %> >
										&#149;
										否
									</option>
								</select>
							</div>
						</div>
						<div class="control-group <%=errors.containsKey("bootstrapping") ? "error" : ""%>">
							<label class="control-label">
								引导节点：
							</label>
							<div class="controls">
								<select name="bootstrapping">
									<option value="true" <%=bootstrapping?" selected=\"selected\" ":"" %> >
										&#149;
										是
									</option>
									<option value="false" <%=!bootstrapping?" selected=\"selected\" ":"" %> >
										&#149;
										否
									</option>
								</select>
								<span class="help-block">集群中首先启动的节点成员.</span>
							</div>
						</div>
					</div>

					<div class="form-actions">
						<input type="button" value="上一步" id="jawa-setup-prev" class="btn btn-info "  onclick="window.location.href='setup-datasource-settings.jsp'" />
						<input type="submit" name="continue" value="下一步" id="jawa-setup-next" class="btn btn-primary " />
					</div>

				</form>
			</div>
		</div>
		<script>
			function changeCache(obj){
				if(obj.value=='true'){
					document.getElementById("clusterSeting").style.display="";
				}else{
					document.getElementById("clusterSeting").style.display="none";
				}
			}
		</script>
	</body>
</html>
